package cn.tedu.datamanager.controller;


import cn.tedu.datamanager.pojo.dto.AdminAddNewDTO;
import cn.tedu.datamanager.pojo.dto.AdminLoginDTO;
import cn.tedu.datamanager.pojo.dto.ServicesAddNewDTO;
import cn.tedu.datamanager.pojo.vo.AdminListItemVO;
import cn.tedu.datamanager.pojo.vo.LevelListItemVO;
import cn.tedu.datamanager.pojo.vo.ServeListItemVO;
import cn.tedu.datamanager.security.LoginPrincipal;
import cn.tedu.datamanager.serve.IAdminService;
import cn.tedu.datamanager.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;


import java.util.List;

@Slf4j
@Api(tags = "03. 员工管理模块")
@RestController
@RequestMapping("/admins")
public class AdminController {

    @Autowired
    IAdminService adminService;

    public AdminController() {
        log.info("创建控制器对象：AdminController");
    }


    // http://localhost:9081/service
    @ApiOperation("查询员工列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    @PreAuthorize("hasAuthority('/sys-admin/temp/admin/read')")
    public JsonResult<List<AdminListItemVO>> list() {
        log.debug("开始处理【查询员工列表】的请求");
        List<AdminListItemVO> list = adminService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:9081/admins/login
    @ApiOperation("员工登录")
    @ApiOperationSupport(order = 50)
    @PostMapping("/login")
    public JsonResult<String> login(AdminLoginDTO adminLoginDTO) {
        log.debug("开始处理【员工登录】的请求，参数：{}", adminLoginDTO);
        String jwt = adminService.login(adminLoginDTO);
        return JsonResult.ok(jwt);
    }

    // http://localhost:9081/admins/xx/enable
    @ApiOperation("启用员工")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "员工id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/enable")
    @PreAuthorize("hasAuthority('/sys-admin/temp/admin/update')")
    public JsonResult<Void> setEnable(@PathVariable Long id) {
        log.debug("开始处理【启用员工】的请求，参数：{}", id);
        adminService.setEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:9081/admins/xx/disable
    @ApiOperation("停用员工")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name = "id", value = "员工id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    @PreAuthorize("hasAuthority('/sys-admin/temp/admin/update')")
    public JsonResult<Void> setDisable(@PathVariable Long id) {
        log.debug("开始处理【停用员工】的请求，参数：{}", id);
        adminService.setDisable(id);
        return JsonResult.ok();
    }

    // http://localhost:9081/admins/add-new?username=aa&phone=bb&email=cc
    @ApiOperation("添加员工")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/sys-admin/temp/admin/add-new')")
    public JsonResult addNew(AdminAddNewDTO adminAddNewDTO) {
        log.debug("开始处理【添加员工】的请求，参数：{}", adminAddNewDTO);
        adminService.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:8080/admins/9527/delete
    @ApiOperation("删除员工")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "员工id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/sys-admin/temp/admin/delete')")
    public JsonResult<Void> delete(@PathVariable Long id,
    @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("开始处理【删除员工】的请求，参数：{}", id);
        adminService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9081/service
    @ApiOperation("修改前查询")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParam(name = "Id", value = "服务项目id", required = true, dataType = "long")
    @PostMapping("/selectupdate/{Id:[0-9]+}")
    public JsonResult selectupdate(@PathVariable Long Id) {
        log.debug("开始处理【查询服务项目列表】的请求");
        return JsonResult.ok(adminService.selectupdate(Id));
    }

    // http://localhost:9081/services/update/2
    @ApiOperation("修改员工数据")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParam(name = "Id", value = "员工id", required = true, dataType = "long")
    @PostMapping("/{Id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/sys-admin/temp/admin/update')")
    public JsonResult update(@Validated AdminAddNewDTO adminAddNewDTO) {
        log.debug("开始处理【修改员工数据】的请求，参数：{}", adminAddNewDTO);
        adminService.update(adminAddNewDTO);
        return JsonResult.ok();
    }


    // "http://localhost:9081/admins/level"
    @ApiOperation("查询员工职位列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("/level")
    public JsonResult<List<LevelListItemVO>> levellist() {
        log.debug("开始处理【查询项目类别列表】的请求");
        List<LevelListItemVO> list = adminService.levellist();
        return JsonResult.ok(list);
    }



}
